<template>
  <div class="el-select">
    <input
      class="el-input__inner"
      type="text"
      :readonly="filterable ? null : 'readonly'"
      v-model="label"
      :placeholder="placeholder"
      :style="{ borderColor: active ? '#409eff' : '#dcdfe6' }"
      @focus="_focus"
      @blur="_blur"
      @input="_input"
    />
    <transition name="dropdown">
      <div class="el-select-dropdown el-scrollbar_view" v-if="active">
        <div class="el-select-dropdown_list">
          <div
            class="el-select-dropdown_item"
            v-for="(item, index) in selectList"
            :key="index"
            @click="_onchange(item)"
          >
            <span :class="{ activeText: item[option.value] === value }">{{
              item[option.label]
            }}</span>
          </div>
        </div>
      </div>
    </transition>
  </div>
</template>

<script>
export default {
  name: "select-option",
  props: {
    value: [String, Number],
    type: {
      type: String,
    },
    filterable: true,
    filterkey: {
      type: String,
      required: false,
      default: "label",
    },
    data: {
      type: Array,
      required: true,
      default() {
        return new Array();
      },
    },
    // data: {
    //   type: Array,
    //   required: false,
    //   default() {
    //     return [
    //       {
    //         label: "水平",
    //         value: "horizontal",
    //       },
    //       {
    //         label: "垂直",
    //         value: "vertical",
    //       },
    //     ];
    //   },
    // },
    option: {
      type: Object,
      required: false,
      default() {
        return {
          value: "value",
          label: "label",
        };
      },
    },
    placeholder: {
      type: String,
      required: false,
      default: "请选择",
    },
  },
  model: {
    prop: "value",
    event: "selected",
  },
  watch: {
    label(val) {
      const data = this.data.find((item) => {
        return item[this.option.label] === val;
      });
      if (data) {
        this.$emit("selected", data[this.option.value]);
      }
    },
  },
  computed: {
    selectList() {
      return this.data;
    },
  },
  data() {
    return {
      label: null,
      active: false,
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      const data = this.data.find((item) => {
        return item[this.option.value] === this.value;
      });
      if (data) {
        this.label = data[this.option.label];
        this.$emit("change", data);
      }
    },
    _focus() {
      this.active = true;
      this.$emit("focus");
    },
    close() {
      this.active = false;
    },
    _blur() {
      // this.active = false
      this.$emit("blur");
    },
    _input(event) {
      let val = event.target.value;
      let key = this.option[this.filterkey];
      if (val != "") {
        const data = this.data.filter((item) => {
          return item[key].indexOf(val) > -1;
        });
        this.selectList = data;
      } else {
        this.label = null;
        this.selectList = this.data;
      }
    },
    _onchange(data) {
      this.label = data[this.option.label];
      console.log(data);
      this.active = false;
      this.$emit("change", {
        key: this.type,
        value: data.value,
      });
    },
  },
};
</script>

<style scoped lang="scss">
.el-select {
  position: relative;
  display: inline-block;
  width: 100%;
  border: 1px solid #fff;
  //   padding: 0 4px;

  & input {
    background: none;
    border: 0px;
    outline: 0px;
    font-size: 12px;
    color: var(--color);
    width: 100%;
    height: 24px;
    line-height: 24px;
  }

  &:hover {
    border: 1px solid var(--border-color);
  }
}

.el-input__inner {
  padding: 0 4px;
}

.el-scrollbar_view {
  overflow-y: auto;
  overflow-x: hidden;

  &::-webkit-scrollbar {
    width: 4px;
  }

  &::-webkit-scrollbar-thumb {
    border-radius: 2px;
    background: rgba(0, 0, 0, 0.1);
  }
}

.el-select-dropdown {
  position: absolute;
  top: 10px;
  z-index: 1025;
  width: 100%;
  max-height: 160px;
  border: 1px solid #e4e7ed;
  border-radius: 4px;
  background-color: #fff;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  box-sizing: border-box;
  margin: 20px 0 10px 0;

  .el-select-dropdown_list {
    .activeText {
      color: #409eff;
    }

    .el-select-dropdown_item {
      font-size: 14px;
      padding: 0 20px;
      position: relative;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      color: #606266;
      height: 34px;
      line-height: 34px;
      box-sizing: border-box;
      cursor: pointer;

      &:hover {
        background-color: #f5f7fa;
      }
    }
  }
}

.dropdown-enter-from,
.dropdown-leave-from {
  transform: translateY(-120px);
}

.dropdown-enter-active,
.dropdown-leave-active {
  transition: all 0.28s;
}

.dropdown-enter-to {
  transform: translateY(0px);
}

.dropdown-leave-to {
  transform: translateY(0px);
}
</style>
